package com.generator;

import com.generator.common.util.ApplicationContextProvider;
import com.generator.common.util.DBConnectionPool;
import com.generator.common.util.SingletonMap;
import com.generator.view.controller.MainUIController;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.apache.log4j.Logger;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;

import javax.swing.*;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.CompletableFuture;

@SpringBootApplication
@MapperScan("com.generator.common.dao")
public class GeneratorApplication extends Application {
    @Override
    public void stop() throws Exception {
        super.stop();
    }
    private static final Logger LOG = Logger.getLogger(GeneratorApplication.class);
    public static Stage mainStage;

    public static ConfigurableApplicationContext applicationContext;

    @Override
    public void init() throws Exception {
        CompletableFuture.supplyAsync(() -> {
            ConfigurableApplicationContext ctx = SpringApplication.run(this.getClass());
            LOG.info("ctx:" + ctx.toString());
            DBConnectionPool.dbUrl = ctx.getEnvironment().getProperty("spring.datasource.url");
            DBConnectionPool.dbUsername = ctx.getEnvironment().getProperty("spring.datasource.username");
            DBConnectionPool.dbPassword = ctx.getEnvironment().getProperty("spring.datasource.password");
            return ctx;
        }).thenAccept(item -> {
            this.launchApplicationView(item);
        });
    }

    private void launchApplicationView(ConfigurableApplicationContext ctx) {
        GeneratorApplication.applicationContext = ctx;
    }

    private Stage primaryStage;

    @Override
    public void start(Stage primaryStage){
        this.primaryStage = primaryStage;
        mainStage = primaryStage;
        URL url = Thread.currentThread().getContextClassLoader().getResource("fxml/MainUI.fxml");
        FXMLLoader fxmlLoader = new FXMLLoader(url);
        Parent root = null;
        try {
            root = fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
        primaryStage.setResizable(false);
        primaryStage.setTitle("mybatis生成工具");
        primaryStage.setScene(new Scene(root));
        primaryStage.show();

        MainUIController controller = fxmlLoader.getController();
        controller.setParent(root);
        primaryStage.show();

        LOG.info("SSS:"+ ApplicationContextProvider.getApplicationContext());
        LOG.info("==========s====:kkk:" + SingletonMap.getInstance().getValueByKey("kkk"));
    }

    public static void main(String[] args) {
        String version = System.getProperty("java.version");
        LOG.info("java  version  " + version);
        if (Integer.parseInt(version.substring(2, 3)) >= 8 || Integer.parseInt(version.substring(0, 1)) >= 9) {
            launch(args);
        } else {
            JFrame jFrame = new JFrame("版本错误");
            jFrame.setSize(500, 100);
            jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel jPanel = new JPanel();
            JLabel jLabel = new JLabel("JDK的版本不能低于1.8请升级至最近的JDK 1.8再运行此软件");
            jPanel.add(jLabel);
            jFrame.add(jPanel);
            jFrame.setLocationRelativeTo(null);
            jFrame.setVisible(true);

        }
    }
}
